<template>
    <el-dialog :title="!dataForm.roleId ? '新增' : '修改'" :visible.sync="visible" destroy-on-close
        :close-on-click-modal="false" :before-close="closeDailog">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="100px">
            <el-form-item label="所属集团" prop="pkGroup">
                <ec-group-tree-select v-model="dataForm.pkGroup" placeholder="请选择所属集团">
                </ec-group-tree-select>
            </el-form-item>
            <el-form-item label="所属组织" prop="pkOrg">
                <ec-org-tree-select :pkGroup="dataForm.pkGroup" v-model="dataForm.pkOrg" placeholder="请选择所属组织">
                </ec-org-tree-select>
            </el-form-item>
            <el-form-item label="角色标识" prop="roleName">
                <el-input v-model="dataForm.roleName" placeholder="角色标识(英文字符串)" :disabled="dataForm.roleId != 0">
                </el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="roleDescription">
                <el-input v-model="dataForm.roleDescription" placeholder="角色描述"></el-input>
            </el-form-item>
            <el-form-item label="角色状态" prop="roleStatus">
                <ec-dict-select v-model="dataForm.roleStatus" placeholder="请选择角色状态" dictType="use_status">
                </ec-dict-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as roleService from '@/api/upms/role'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    roleId: 0,
                    pkGroup: "",
                    pkOrg: "",
                    roleName: "",
                    roleType: "sys",
                    roleDescription: "",
                    roleStatus: "Y",
                    revision: "",
                },
                dataRule: {
                    pkGroup: [{
                        required: true,
                        message: '所属集团不能为空',
                        trigger: 'change'
                    }],
                    pkOrg: [{
                        required: true,
                        message: '所属组织不能为空',
                        trigger: 'change'
                    }],
                    roleName: [{
                        required: true,
                        message: '角色标识不能为空',
                        trigger: 'blur'
                    }],
                    roleDescription: [{
                        required: true,
                        message: '角色描述不能为空',
                        trigger: 'blur'
                    }],
                    roleStatus: [{
                        required: true,
                        message: '角色状态不能为空',
                        trigger: 'blur'
                    }],
                },
            }
        },
        methods: {
            init(id) {
                this.dataForm.roleId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    if (this.dataForm.roleId) {
                        roleService.getroleinfo(this.dataForm.roleId).then(data => {
                            this.dataForm.pkGroup = data.pkGroup
                            this.dataForm.pkOrg = data.pkOrg
                            this.dataForm.roleName = data.roleName
                            this.dataForm.roleType = data.roleType
                            this.dataForm.roleDescription = data.roleDescription
                            this.dataForm.roleStatus = data.roleStatus
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'roleId': this.dataForm.roleId || undefined,
                            'pkGroup': this.dataForm.pkGroup,
                            'pkOrg': this.dataForm.pkOrg,
                            'roleName': this.dataForm.roleName,
                            'roleType': this.dataForm.roleType,
                            'roleDescription': this.dataForm.roleDescription,
                            'roleStatus': this.dataForm.roleStatus,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.roleId ? roleService.addsysrole(params) : roleService
                            .updaterole(
                                params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            }
        }
    }
</script>